package cn.net.zidian.mvvm.activity;

import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;

import androidx.lifecycle.Observer;

import com.blankj.utilcode.util.RegexUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;

import cn.net.zidian.bean.CiDianBean;
import cn.net.zidian.bean.HistoryBean;
import cn.net.zidian.bean.IdCardBean;
import cn.net.zidian.databinding.ActivityCidianSearchBinding;
import cn.net.zidian.databinding.ActivityIdcardSearchBinding;
import cn.net.zidian.mvvm.vm.CiDianVM;
import cn.net.zidian.mvvm.vm.IdCardVM;
import cn.net.zidian.util.HistoryData;

public class CiDianActivity extends BaseActivity<CiDianVM> {

    private ActivityCidianSearchBinding viewBinding;

    @Override
    protected CiDianVM viewModel() {
        return new CiDianVM();
    }

    @Override
    protected void initView() {
        viewBinding = ActivityCidianSearchBinding.inflate(LayoutInflater.from(this));
        setContentView(viewBinding.getRoot());
        viewBinding.titleBar.getLeftView().setOnClickListener(v -> finish());
        initObserver();
        viewBinding.tvSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String word = viewBinding.etSearch.getText().toString();
                if (!StringUtils.isEmpty(word)) {
                    HistoryData.list.add(new HistoryBean(word, "词典"));
                    vm.getData(word);
                } else {
                    ToastUtils.showShort("请输入后搜索");
                }
            }
        });
    }

    private void initObserver() {
        vm.eventList.observe(this, new Observer<CiDianBean>() {
            @Override
            public void onChanged(CiDianBean bean) {
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.append("名称:").append(bean.getName())
                        .append("\n").append("拼音:").append(bean.getPinyin())
                        .append("\n").append("内容:");
                viewBinding.tvContent.setText(stringBuilder.toString() + Html.fromHtml(bean.getExample()) + "\n" + Html.fromHtml(bean.getExample()));
            }
        });

    }


}
